<template>
  <div>
    <h3>用户管理</h3>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                  v-model="form.realname"
                  style="width: 100%"
                  placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关键码">
              <el-input
                  v-model="form.phone"
                  style="width: 100%"
                  placeholder="手机号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-date-picker
                  v-model="createTimes"
                  type="datetimerange"
                  range-separator="To"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  format="YYYY-MM-DD HH:mm:ss"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
          <!--添加按钮-->
          <div class="tools-div">
            <el-button size="small" type="success" @click="overdueUser">禁用</el-button>
            <el-button size="small" type="success" @click="enableUser">用户</el-button>
<!--            <el-badge :value="depositTotal" class="item" >-->
<!--              <el-button @click="showDeposit">押金审核</el-button>-->
<!--            </el-badge>-->
          </div>
        </el-row>
      </el-form>
    </div>
<!--    <h1 v-show="showDepositFlag">哈哈</h1>-->
    <!---数据表格-->
    <el-table :data="list" v-show="fectUserFlag" style="width: 100%">
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="sex" label="性别" #default="scope" >
        {{scope.row.sex == 0 ? '男' : '女'}}
      </el-table-column>
      <el-table-column prop="phone" label="手机号" #default="scope">
          {{scope.row.phone.substring(0,3)}}******{{scope.row.phone.substring(7,11)}}
      </el-table-column>
      <el-table-column prop="cardid" label="身份证" #default="scope">
          {{scope.row.cardid.substring(0,6)}}******{{scope.row.cardid.substring(14,18)}}
      </el-table-column>
      <el-table-column prop="createtime" label="在线时间" />
      <el-table-column prop="integral" label="积分" />
      <el-table-column prop="deposit" label="押金" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="danger" size="small" @click="updateById(scope.row)">
          {{ scope.row.state == 0 ? '禁用' : '启用' }}
        </el-button>
        <el-button  :disabled="scope.row.state===1" type="danger" size="small" @click="returnDeposit(scope.row)">
          {{ scope.row.state === 0 ? '退还':'该按钮不能用' }}
        </el-button>
      </el-table-column>
    </el-table>
    <!--分页条-->
    <el-pagination v-show="fectUserFlag"
        v-model:current-page="pageParamsList.page"
        v-model:page-size="pageParamsList.limit"
        :page-sizes="[2, 20, 50, 100]"
        @size-change="fetchSysUser"
        @current-change="fetchSysUser"
        layout="total, sizes, prev, pager, next"
        :total="total"
    />

    <!---数据表格-->
<!--    <el-table :data="list" v-show="showDepositFlag" style="width: 100%">-->
<!--      <el-table-column prop="realname" label="姓名" />-->
<!--      <el-table-column prop="sex" label="性别" #default="scope" >-->
<!--        {{scope.row.sex == 0 ? '男' : '女'}}-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="phone" label="手机号" #default="scope">-->
<!--        {{scope.row.phone.substring(0,3)}}******{{scope.row.phone.substring(7,11)}}-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="cardid" label="身份证" #default="scope">-->
<!--        {{scope.row.cardid.substring(0,6)}}******{{scope.row.cardid.substring(14,18)}}-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="createtime" label= "在线时间" />-->
<!--      <el-table-column prop="integral" label="积分" />-->
<!--      <el-table-column prop="deposit" label="押金" />-->
<!--      <el-table-column label="操作" align="center" width="280" #default="scope">-->
<!--        <el-button type="danger" size="small" @click="updateById(scope.row)">-->
<!--          {{ scope.row.state == 0 ? '禁用' : '启用' }}-->
<!--        </el-button>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

  </div>

</template>

<script setup>
import {onMounted, ref} from "vue";
import {getDepositUserList, getReturnDeposit, getUpdateState, getUserList} from "@/api/changebattery/userManager";
import {ElMessage, ElMessageBox} from "element-plus";

//开始和结束时间数据模型
const createTimes = ref([])

//记录数
const depositTotal=ref(0)

const list = ref([])

const queryDto = {
  realname: '',
  createTimeBegin: '',
  createTimeEnd: '',
  phone:'',
  state:0
}



//退还押金
const returnDeposit = row =>{
  ElMessageBox.confirm('此操作将退还该用户的押金, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getReturnDeposit(row.id)
    if (code === 200) {
      ElMessage.success('操作成功')
      await fetchSysUser()
    }
  })
}

const form = ref(queryDto)
//重置
const resetData = async () => {
  form.value.realname = ''
  form.value.phone = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  createTimes.value=[]
  await fetchSysUser()
}
//启用用户
const enableUser = () =>{
  form.value.state=0
  fetchSysUser()
}
//否用用户
const overdueUser = () =>{
  form.value.state=1
  fetchSysUser()
}
const fectUserFlag=ref(false)
const showDepositFlag=ref(false)
const showDeposit =  async () =>{
  showDepositFlag.value=true
  fectUserFlag.value=false
}

//上架下架
const updateById = async (row) => {
  const { code } = await getUpdateState(row.id)
    ElMessage.success('操作成功')
   await fetchSysUser(pageParamsUsers.value.page,
        pageParamsUsers.value.limit,
        form.value)
}

//搜索方法
const searchSysUser = () => {
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

const depositList = ref([])

//退款人员
const fetchDepositUserList = async () => {
  const { data } = await getDepositUserList()
  depositTotal.value=data.length
  depositList.value=data
}
//列表
const fetchSysUser = async () => {
  fectUserFlag.value=true
  showDepositFlag.value=false
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getUserList(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
  list.value = data.userManager.records
  total.value = data.userManager.total
}
onMounted(async ()=>{
  await fetchSysUser()
  await fetchDepositUserList()
})
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>
